import axios from "axios";
import { getToken, delToken } from "@/utils/local-storage";
import { Toast } from "vant";
import router from "@/router";
const instance = axios.create({
  baseURL: "http://interview-api-t.itheima.net/",
  timeout: 4000,
});

instance.interceptors.request.use(
  function (config) {
    const token = getToken();
    if (token) {
      config.headers.Authorization = "Bearer " + token;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);
instance.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    const response = error.response;
    // console.log(response)
    // if (response.status == 400) {
    //   Toast.fail(response.data.message);
    // } else {
    //   delToken();
    //   Toast.fail("登录超时，请重新登录");
    //   router.go(0);
    // }
    return Promise.reject(error);
  }
);

export default instance;
